//
//  LSPhoneVerificationVController.m
//  LookScore
//
//  Created by wr on 2017/12/7.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSPhoneVerificationVController.h"
#import "JKCountDownButton.h"
#import "LSAccountCoder.h"
#import "LSUserAgreeViewController.h"
@interface LSPhoneVerificationVController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;

@property (weak, nonatomic) IBOutlet UITextField *verifitionTF;

@property (weak, nonatomic) IBOutlet UIButton *sureBu;

@property (weak, nonatomic) IBOutlet JKCountDownButton *verificationBu;

@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIImageView *passwordImgaeview;
@property (weak, nonatomic) IBOutlet UIView *passworldView;
@property (nonatomic, assign) BOOL isHavePWD;
@end

@implementation LSPhoneVerificationVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.title=@"手机验证";
    self.edgesForExtendedLayout = NO;
    
//    self.passwordTF.height = 1;
//    self.passwordImgaeview.hidden = YES;
    
//    [self.verificationBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor9dd3cc] forState:UIControlStateNormal];
    [self.verificationBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateHighlighted];
    
//    [self.sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor9dd3cc] forState:UIControlStateNormal];
    [self.sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateHighlighted];
    
    [self.verificationBu countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        sender.enabled = NO;
        
        [sender startCountDownWithSecond:60];
        
        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
            NSString *title = [NSString stringWithFormat:@"剩余%zds",second];
            return title;
        }];
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            return @"重新获取";
            
        }];
        
    }];
}

#pragma mark 🔥 事件

- (IBAction)verificationAction:(JKCountDownButton *)sender {
    
    if (_phoneTF.text.length == 0) {
        [MBProgressHUD showOnlyMessage:@"请输入手机号" afterDelay:1.5];
        return;
    }
    
    [self.verificationBu countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        sender.enabled = NO;
        
        [sender startCountDownWithSecond:60];
        
        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
            NSString *title = [NSString stringWithFormat:@"剩余%zds",second];
            return title;
        }];
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            return @"重新获取";
            
        }];
        
    }];
    
    
    
    
    [LSHttpEnclosureTool POST:LSUserOtherLoginValidate parameters:@{@"mobile":_phoneTF.text} resBlock:^(id responseObject) {
        MyLog(@"%@",responseObject);
        if (responseObject) {
            if ([responseObject boolValue]) {
                self.passwordImgaeview.hidden = YES;
                self.passwordTF.hidden = YES;
                self.passworldView.hidden = YES;
                _isHavePWD = YES;
            }else{
                self.passwordImgaeview.hidden = NO;
                self.passwordTF.hidden = NO;
                self.passworldView.hidden = NO;
                _isHavePWD = NO;
            }
        }
    }];

    
}

- (IBAction)sureAction:(UIButton *)sender {
    if (_verifitionTF.text.length != 0 && _phoneTF.text.length != 0) {
        NSDictionary * dict;
        
        if (_isHavePWD) {
            dict= @{@"unionId":self.openId,@"headUrl":self.headUrl,@"disName":self.disName,@"regType":self.regType,@"mobile":_phoneTF.text,@"validateCode":_verifitionTF.text,@"loginPwd":@"null"};
            [self loginWith:dict];
        }else{
            if (_passwordTF.text.length == 0) {
                [MBProgressHUD showOnlyMessage:@"请输入密码" afterDelay:1.5];
                return;
            }
           dict= @{@"unionId":self.openId,@"headUrl":self.headUrl,@"disName":self.disName,@"regType":self.regType,@"mobile":_phoneTF.text,@"validateCode":_verifitionTF.text,@"loginPwd":_passwordTF.text};
            [self loginWith:dict];
        }
    }else{
       [MBProgressHUD showOnlyMessage:@"请输入相应的" afterDelay:1.5];
        
    }
    
}

- (void)loginWith:(NSDictionary *)dict{
    
    [LSHttpEnclosureTool POST:LSUserOtherlogin parameters:dict resBlock:^(id responseObject) {
        if (responseObject) {
            LSAccountCoder *coderModel=[LSAccountCoder mj_objectWithKeyValues:responseObject];
            if (self.block) {
                self.block(coderModel);
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    
}

- (IBAction)protocolAction:(UIButton *)sender {
    LSUserAgreeViewController * userAgreeVC = [[LSUserAgreeViewController alloc] init];
    userAgreeVC.urlStr = @"http://www.kanbf.com/UserAgreement.html";
    userAgreeVC.titleStr = @"用户协议";
    [self.navigationController pushViewController:userAgreeVC animated:YES];
}


- (IBAction)privacyAction:(UIButton *)sender {
    LSUserAgreeViewController * userAgreeVC = [[LSUserAgreeViewController alloc] init];
    userAgreeVC.urlStr = @"http://www.kanbf.com/Privacypolicy.html";
    userAgreeVC.titleStr = @"用户协议";
    [self.navigationController pushViewController:userAgreeVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
